package com.example.side_pull_menu.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.side_pull_menu.Fragmentkj2;
import com.example.side_pull_menu.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;

import static android.support.constraint.Constraints.TAG;

public class Fragment_2 extends Fragment {
    private TextView tv_carno;
    private TextView tv_who;
    private TextView tv_carplate;
    private ListView lv;
    private List<Fragmentkj2> fragment2kjs = new ArrayList<Fragmentkj2>();
    private BaseAdapter adapter;
    public Fragment_2() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view1 = inflater.inflate(R.layout.fragment_2, container, false);
        return view1;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getpost();

    }
    private void initView() {
        tv_carno = (TextView) getActivity().findViewById(R.id.tv_carno);
        tv_who = (TextView) getActivity().findViewById(R.id.tv_who);
        tv_carplate = (TextView) getActivity().findViewById(R.id.tv_carplate);
        lv = (ListView) getActivity().findViewById(R.id.lv);
    }
    public interface BlogService {
        @GET("carlist")
        Call<ResponseBody> getBlog();
    }
    public void getpost() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        RequestBody requestBody = (RequestBody) retrofit.create(BlogService.class);
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Gson gson1 = new Gson();
                    Fragmentkj2 carlist=gson1.fromJson(response.body().string(),Fragmentkj2.class);
                    fragment2kjs.add(carlist);
                    lv.setAdapter( new MyAdapter());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return fragment2kjs.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Log.i("TAG","1234"+new Gson().toJson(fragment2kjs));
            View view1=(View)View.inflate(getContext(),R.layout.fragment_2,null);
            TextView who=view1.findViewById(R.id.tv_who);
            TextView carno=view1.findViewById(R.id.tv_carno);
            TextView carplate=view1.findViewById(R.id.tv_carplate);
            who.setText(fragment2kjs.get(0).getData().get(i).getWho());
           carno.setText(fragment2kjs.get(0).getData().get(i).getCarno());
           carplate.setText(fragment2kjs.get(0).getData().get(i).getCarplate());
            return view1;
        }
    }
}
